/**
 * 阿有 email:37798955@qq.com
 * 2017-11
 */
//fmt='yyyy-MM-dd hh:mm:ss'

var MYMODEL = null;


////时间戳转日期
//Date.prototype.getLocalTime = function(lt) {
//	return new Date(parseInt(nS) * 1000).toLocaleString().replace(/:\d{1,2}$/, ' ');
//}
////日期转时间戳
//Date.prototype.setLocalTime = function(datestr) {
//	return(new Date(datestr)).getTime() / 1000;
//}

//var oldTime = (new Date("2015/06/23 08:00:20")).getTime() / 1000;

var DSQL=new (
	function(){		
		//???? 保存到文本文件
		var dbpath='_doc/db/';
		var fi=new Ffile();
		this.setItem=function(key,value){
			fi.writeFile(dbpath+key,JSON.stringify(value));
		}
		this.getItem=function(key,callback){
			fi.readData(dbpath+key,function(value){
				if (callback)
					callback(JSON.parse(value));
			},'utf-8');
		}
		
//		this.setItem=function (key, value) {
//			if(key) {
//				if(window.plus) {					
//					plus.storage.setItem(key, JSON.stringify(value));
//				} else {
//					window.localStorage[key] = JSON.stringify(value);
//				}
//			}
//		}
//		
//		this.getItem=function (key) {
//			if(key) {
//				var obj = null;
//				if(window.plus) {					
//					obj = plus.storage.getItem(key);
//				} else {
//					obj = window.localStorage[key];
//				}
//				if(obj)
//					return JSON.parse(obj);
//			}
//			return null;
//		}	
	}
);
/**
 * 邹老师的前端 数据存储器 V1.0 
 * qq:37798955@qq.com
 * @param {Object} tblname
 * @param {Object} model
 * @param {Object} idtype
 */
function XSQL(tblname, model, idtype) {
	this._tableName = "";
	this._err = '';
	this._where = null;
	this._select = null;
	this._model = null;
	//this._rs = ''; //结果集合 不能定义 因为很多结果会冲突
	this._columns = [];
	this._idtype = 'G';
	this.limitBegin = 0; //从哪个位置开始读取
	this.limitLen = -1; //n=-1 读取所有，n>-1 读取n条
	var _backuppath = 'backup.f'; //备份文件
	//初始化数据库
	this.init = function(tblname, model, idtype) {
		this._tableName = tblname;

		if(model == null) {
			if(MYMODEL && (tblname in MYMODEL)) {
				model = MYMODEL[tblname]
				model['_idkey'] = ''; //唯一编号
				model['_datekey'] = ''; //自动时间分区
				model['_syschgdate'] = ''; //变更时间
				this._model = model;
			}
		}
		//console.log("573"+tblname+JSON.stringify(model));

		//this._rs = arr;

		//自增类型还是guid类型
		if(idtype == null)
			this._idtype = 'G'
		else
			this._idtype = idtype.toUpperCase();
	}
	if(tblname)
		this.init(tblname, model, idtype);

	function update(key) {
		//alert('zzz')
		
		DSQL.getItem('ALLTABLESTATUS',
		function(mydb){
			if(mydb == null) mydb = {};
			mydb[key] = new Date().Format();
			DSQL.setItem('ALLTABLESTATUS', mydb);
		});
		
	}

	function getLen() {
		if(window.plus)
			return plus.storage.getLength();
		else
			return window.localStorage.length;
	}

	function removeItem(key) {
		if(window.plus)
			plus.storage.removeItem(key);
		else
			window.localStorage.removeItem(key);
	}

	function key(index) {
		if(window.plus)
			plus.storage.key(index);
		else
			window.localStorage.key(index);
	}

	//结构KEYID:{tablename:id}
	function getAutoID(tableName, idtype) {
		var idmap = DSQL.getItem("KEYID"); //专门存放自增的keyid
		if(idmap == null)
			idmap = [];
		var oid = idmap[tableName];
		if(oid == null)
			oid = 0;
		var nid;
		//console.log(idtype)
		if(idtype == 'G') //init()
		{
			nid = getGuid();
			//console.log(nid)
		} else {
			nid = parseInt(oid) + 1;
			//console.log(nid)
		}

		idmap[tableName] = nid;
		DSQL.setItem("KEYID", idmap);
		return oid, nid;
	}

	this.onErr = function(tx, e) {
		this._err = e;
		console.log(e.message);
	}
	this.batchreplace = function(callback) {
		var sub = DSQL.getItem(this._tableName);
		if(callback)
			sub = callback(sub)
		DSQL.setItem(this._tableName, sub);
	}
	//模型：当前库和历史库
	//当前库表名=key，记录是[]
	//当前库表名为_datekey：表名=key，记录是[]
	//_idkey 和_datekey 是插件的关键属性 ，自定义属性不能用_开头
	//修改当前对象
	this.setObj = function(obj) {

		//console.log('214:' + obj._idkey)
		var sub = DSQL.getItem(this._tableName);
		if(_IS.isEmptyString(obj._idkey)) { //等于空时 自动赋值id
			obj._idkey = getAutoID(this._tableName, this._idtype);
		}
		//console.log('218:' + obj._idkey)
		obj._syschgdate = new Date().Format()
		if(sub == null)
			sub = {}
		delete sub[""];
		sub[obj._idkey] = obj;
		//console.log(this._tableName)
		//console.log(this._idtype )
		//log(obj)
		DSQL.setItem(this._tableName, sub);
		update(this._tableName);
		return obj._idkey
	}
	this.delObjById = function(id) {
		var sub = DSQL.getItem(this._tableName);
		delete sub[id];
		DSQL.setItem(this._tableName, sub);
	}
	this.clear = function() {
		DSQL.setItem(this._tableName, {});
	}
	this.setList = function(rs) {
		for(i in rs) {
			this.setObj(rs[i])
		}
	}
	//修改历史对象
	function setHisObj(obj) {
		//(new Date()).format('yyyyMM')
		var key = obj._datekey + ":" + this._tableName;
		var sub = DSQL.getItem(key);
		obj._syschgdate = new date().Format()
		sub[obj._idkey] = obj;
		update(obj._idkey);
		DSQL.setItem(key, sub);
	}
	this.setHisList = function(rs) {
		for(i in rs) {
			setHisObj(rs[i])
		}
	}
	//移动
	this.mov2His = function(obj) {
		getObj(obj)
		setHisObj(obj)
	}

	function getListByIDs(tablename, idkeys) {
		var arr = []
		var sub = DSQL.getItem(tablename); //sub={} or map	
		//console.log('getListByIDs')
		//console.log(tablename)
		//log(sub)
		var key;
		if(sub == null) return null;
		if(idkeys instanceof Array && idkeys.length > 0) {
			for(i in idkeys) {
				key = idkeys[i]
				if(sub.hasOwnProperty(key))
					arr.push(sub[key]);
			}
			//console.log("cccc"+key+sub[key])
		} else if(typeof(idkeys) == 'string') {

			if(sub.hasOwnProperty(idkeys))
				//arr[idkeys]=sub[idkeys];
				arr.push(sub[idkeys]);
		}
		return arr;
	}
	//通过id得到对象
	this.getObjByID = function(id) {
		if(id == null) return null;
		var objs = getListByIDs(this._tableName, id.toString())
		if(objs == null) return null;
		if(objs.length > 0)
			return objs[0];
		return null;
	}
	//通过id得到对象
	this.getListByID = function(ids) {
		return getListByIDs(this._tableName, ids)
	}
	//参数格式： datekey:"";ids:[ids]
	this.getHisListByID = function(datekey, ids) {
		var key = datekey + ":" + this._tableName;
		return getListByIDs(key, ids)
	}
	//通过查询条件筛选内部数据得到对象
	this.getList = function(callback) {
		var sub = DSQL.getItem(this._tableName);
		return this.reduces(sub, callback);
	}
	//通过查询条件得到对象
	this.getHisList = function(datekey) {
		var key = datekey + ":" + this._tableName;
		var sub = DSQL.getItem(key); //[]记录集合
		return this.reduces(sub);
	}

	this.where = function(where) {
		this._where = where;
		return this;
	}
	this.select = function(selectsql) {
		this._select = selectsql;
		return this;
	}
	//限定记录列表区间
	this.limit = function(begin, len) {
		this.limitBegin = begin;
		this.limitLen = len;
		return this;
	}

	//对外部的数据进行筛选
	//rs=map
	this.reduces = function(rs, callback) {
		//if(this._where == null) return rs;
		//		/log(this._where,'jsondb.js647:')
		//console.log(JSON.stringify(this._where))
		var newrs = [];
		//
		if(callback && callback.ini)
			callback.ini();
		if(rs) {
			//for(var i  in rs) {
			var begin = 0;
			var len = 0;
			this.limitBegin = this.limitBegin | 0;
			if(limitBegin > rs.length) {
				this.limitBegin = 0;
			} else {
				begin = this.limitBegin;
			}
			if((!this.limitLen) || this.limitLen < 0 || begin + this.limitLen > rs.length) {
				this.limitLen = rs.length - begin;
			}
			len = this.limitLen;
			for(var i = begin; i < len; i++) {
				if(rs[i]) {
					//console.log("ggg0"+i+JSON.stringify(rs[i]))
					//console.log("ggg0"+rs[i])
					//console.log(this._select);				
					var rc = reduce(rs[i], this._where);
					if(rc) {
						newrs.push(_JO.selectcols(rc, this._select))
						if(callback && callback.ing)
							callback.each(rc, i);
					}
				}
			}
		}
		if(callback && callback.finish)
			callback.finish();
		//console.log("ggg1")
		return newrs;
	}

	//key ='field(','field)','field[','field]','field=','field%','or'
	//,'field[%','field%]' 数组和map 不允许用特殊符号表示大小比较和模糊匹配
	//每条记录的属性与条件进行判断
	function reduce(rc, whereobj) {
		if(rc instanceof Array && rc.length > 0) {
			return null;
		}
		if(whereobj == null) {
			return rc;
		}
		if(whereobj instanceof Array && whereobj.length > 0) {
			for(var i = 0; i < whereobj.length; i++) {
				//for(var i in whereobj) {
				//console.log(JSON.stringify(whereobj[i]))
				var r = reduce(rc, whereobj[i]);
				if(r != null) return r;
			}
			//console.log("dd "+JSON.stringify(whereobj))
			return null;
		} else {
			if(typeof(whereobj) === 'string') { //不支持 string 类型的条件
				return rc;
			}
			for(var key in whereobj) {
				var v = whereobj[key];

				if(v == null || v === '') continue; //如果查询的值为空跳过
				//	console.log(key +' '+v+' '+rcv)

				if(key == "or") {
					//此时的wvalue=[条件]
					if(v instanceof Array && v.length > 0)
						return reduce(rc, v);
				} else {
					var m = '=';
					var n = 0;
					if((n = key.indexOf('=')) > 0)
						m = '=';
					else if((n = key.indexOf('(')) > 0)
						m = '(';
					else if((n = key.indexOf(')')) > 0)
						m = ')';
					else if((n = key.indexOf('[%')) > 0)
						m = '[%';
					else if((n = key.indexOf('%]')) > 0)
						m = '%]';
					else if((n = key.indexOf('[')) > 0)
						m = '[';
					else if((n = key.indexOf(']')) > 0)
						m = ']';
					else if((n = key.indexOf('%')) > 0)
						m = '%';

					if(n == -1)
						n = key.length;

					key = key.slice(0, n).replace(/(^\s*)|(\s*$)/g, "");
					var rcv = rc[key];
					if(_IS.isArray(rcv) || _IS.isMap(rcv)) continue; //如果是数组或者map类型跳过

					//console.log(key+' '+rcv+m+' '+v)

					if(typeof(rcv) == "undefined") {
						return null;
					} else if(typeof(rcv) == 'string') {
						//console.log(m + ' '+typeof(rcv) +' ' + rcv+' '+v+rcv.indexOf(v) )
						if((m == '=' && rcv == v) ||
							(m == '%' && rcv.indexOf(v) > -1) ||
							(m == '[%' && rcv.indexOf(v) == 0) ||
							(m == '%]' && rcv.slice(-v.length) == v) ||
							(m == '(' && rcv > v) || (m == ')' && rcv < v) ||
							(m == '[' && rcv >= v) || (m == ']' && rcv <= v)) {
							continue;
						} else {
							return null;
						}
					} else if(typeof(rcv) === 'number') {
						if((m == '=' && rcv == v) || (m == '(' && rcv > v) ||
							(m == ')' && rcv < v) || (m == '[' && rcv >= v) ||
							(m == ']' && rcv <= v)) {
							//console.log(key+' '+rcv+' '+m+' '+v)
							continue;
						} else {
							return null;
						}
					} else if(_IS.isBool(rcv)) {
						if((m == '=' && rcv == v)) {
							continue;
						} else {
							return null;
						}
					} else {
						console.log('条件表达式错误请检查')
						continue;
					}
				}
			}
			//console.log('ok'+key)
			return rc;
		}
	}
}
//model 在开始的时候插入createdb.html
function hasClass(obj, cls) {
	var obj_class = obj.className, //获取 class 内容.
		obj_class_lst = obj_class.split(/\s+/); //通过split空字符将cls转换成数组.
	x = 0;
	for(x in obj_class_lst) {
		if(obj_class_lst[x] == cls) { //循环数组, 判断是否包含cls
			return true;
		}
	}
	return false;
}

function createMmdl() {
	var obj = {
		_tableName: "",
		_rs: [{
			_data: {},
			_where: {}
		}]
	};
	return obj;
}

function createSmdl() {
	var obj = {
		_tableName: "",
		_rs: [{
			_data: {},
			_where: {}
		}]
	};
	return obj;
}
//测试打印json对象
function log(data, note) {
	var s = JSON.stringify(data);
	if(note == null)
		console.log(s)
	else
		console.log(note + ' ' + s)
}
//测试打印map对象
function printmap(map) {
	for(var i in map) {
		console.log(JSON.stringify(map[i]));
	}
}
//-----------------防止转拼音
//////////////////下拉与数据有关 所以放在这里

var PAGESIZE = 30;
var CURMON = new Date().Format('yyyyMM')
/*
examples:
var db=createdb();
db.init('channel_list',[{name:'id',type:'integer primary key autoincrement'},{name:'name',type:'text'},{name:'link',type:'text'},{name:'cover',type:'text'},{name:'updatetime',type:'integer'},{name:'orders',type:'integer'}]);
db.init('feed_list',[{name:'parentid',type:'integer'},{name:'feed',type:'text'}]);
db.where({name:'aa'}).getData(function(result){
  console.log(result);//result为Array
});
db.where({name:'aa'}).deleteData(function(result){
  console.log(result[0]);//删除条数
});
db.where({name:'bb'}).saveObj({link:'jj'},function(result){
  console.log(result);//影响条数
})
})
*/